<?php

namespace app\models;

use Yii;
use yii\web\IdentityInterface;
use app\models\SysUserLog;

/**
 * This is the model class for table "{{%sys_users}}".
 *
 * @property integer $id
 * @property string $phone
 * @property string $parent_phone
 * @property integer $type
 * @property string $password
 * @property integer $role
 * @property integer $last_login_time
 * @property string $truename
 * @property string $qq
 * @property string $email
 * @property string $weixin
 * @property string $tel
 * @property string $company_name
 * @property string $company_addr
 * @property string $company_regnum
 * @property string $company_range
 * @property string $company_spec_range
 * @property string $company_type
 * @property string $company_est_time
 * @property string $company_licence
 * @property integer $reg_time
 * @property integer $status
 */
class SysUsers extends \yii\db\ActiveRecord implements IdentityInterface {
    const PERSON=0;
    const CAIGOU=1;
    const CAIWU=2;
    const COM_UN=3;
    const COM_PA=4;

        /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%sys_users}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['phone', 'password'], 'required'],
            [['type', 'role', 'last_login_time', 'reg_time', 'status','parent_id'], 'integer'],
            [['phone', 'parent_phone'], 'string', 'max' => 11],
            [['password'], 'string', 'max' => 120],
            [['truename', 'qq', 'tel', 'company_type'], 'string', 'max' => 20],
            [['qq_openid', 'wx_openid'], 'string', 'max' => 255],
            [['email', 'weixin'], 'string', 'max' => 50],
            [['company_name', 'company_regnum'], 'string', 'max' => 40],
            [['company_addr', 'company_licence'], 'string', 'max' => 100],
            [['company_range', 'company_spec_range', 'company_est_time', 'company_type'], 'string', 'max' => 500]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'phone' => '手机号',
            'parent_phone' => '父手机号',
            'type' => '0删除 1没有删除',
            'password' => '密码',
            'role' => '角色：0所有权限 1采购 2财务',
            'last_login_time' => '最后登录时间',
            'truename' => '姓名',
            'qq' => 'QQ',
            'email' => '邮箱地址',
            'weixin' => 'Weixin',
            'tel' => '电话',
            'company_name' => '公司全称',
            'company_addr' => '公司地址',
            'company_regnum' => '公司注册号',
            'company_range' => '一般经营范围',
            'company_spec_range' => '特殊经验范围',
            'company_type' => '公司类型',
            'company_est_time' => '公司成立时间',
            'company_licence' => '公司营业执照',
            'reg_time' => '注册时间',
            'status' => '0 普通用户 1 企业用户 3 企业用户子用户',
             'parent_id' =>'父ID',
        ];
    }

    public static function findIdentity($id) {
        return static::findOne(['id' => $id]);
    }

    public static function findIdentityByAccessToken($token, $type = null) {
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey() {
        return $this->id;
    }

    public function getId() {
        return $this->getPrimaryKey();
    }

    public function validateAuthKey($authKey) {
        return $this->getAuthKey() === $authKey;
    }
    /**
     *getUserByTelAndPassword($tel,$password) 通过手机号和密码获取用户
     * author:sunjian
     * time:2015年12月19日19:09:12
     * return SysUser<object>
     *      */
    static function getUserByTelAndPassword($tel,$password){
        return static::findOne(['phone' => $tel,'password'=>md5($password)]);
    }
    
    /**
     *getUserByTel($tel) 通过手机号获取用户
     * author:sunjian
     * time:2015年12月18日21:39:34
     * return SysUser<object>
     *      */
    static function getUserByTel($tel){
        return static::findOne(['phone' => $tel]);
    }
    /**
     *getUserByCompanyName($company_name) 通过公司名获取用户
     * author:sunjian
     * time:2015年12月19日17:46:41
     * return SysUser<object>
     *      */
    static function getUserByCompanyName($company_name){
        return static::findOne(['company_name' => $company_name]);
    }
    
    /**
     *ResetPassword($phone,$password) 重置密码
     * author:sunjian
     * time:2015年12月20日15:47:08
     * return <bool>
     *      */
    static function ResetPassword($phone,$password){
        $user= static::getUserByTel($phone);
        $user->password=md5($password);
        if($user->save()){
            return true;
        }else{
            return false;
        }
    }
    /**
     * time:2016年1月9日16:24:34
     * author:sunjian
     * 用户日志操作
     */
    public function writeLog(){
        $log = new SysUserLog;
        $log->uid=$this->id;
        $log->operation="登陆";
        $log->create_time=time();
        $log->ip=Yii::$app->getRequest()->getUserIP();
        if($log->save()){
            return true ;
            
        }else{
            return false;
        }
    }
}
